const question = require("../models/question")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');


router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            question.find({
                position: fields.position,
                point_id: fields.point_id
            }, (err, docs) => {
                    if (err) {
                        res.json({ 'state': false, 'msg': err });
                    } else {
                        let idx = Math.floor(Math.random() * docs.length)
                        res.json({ 'state': true, 'result': docs[idx] });
                        return
                    }
                })
        }

    });

});

module.exports = router;